package com.houseman.workFlow.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.houseman.workFlow.bean.Line;
import com.houseman.workFlow.bean.TaskArea;
import com.houseman.workFlow.bean.WorkFlowGraph;
import com.houseman.workFlow.dao.FolderDao;
import com.houseman.workFlow.dao.GraphDao;
import com.houseman.workFlow.dao.LineDao;
import com.houseman.workFlow.dao.TaskAreaDao;
import com.houseman.workFlow.dao.impl.FolderDaoImpl;
import com.houseman.workFlow.dao.impl.GraphDaoImpl;
import com.houseman.workFlow.dao.impl.LineDaoImpl;
import com.houseman.workFlow.dao.impl.TaskAreaDaoImpl;
import com.houseman.workFlow.service.GraphService;

public class GraphServiceImpl implements GraphService {

	//dao
	GraphDao gdao = new GraphDaoImpl();
	TaskAreaDao tdao = new TaskAreaDaoImpl();
	LineDao ldao = new LineDaoImpl();
	FolderDao fdao = new FolderDaoImpl();
	
	/**
	 * 查询流程图信息
	 * @return
	 * @throws Exception
	 */
	public WorkFlowGraph queryWorkFlowGraph(int graphId) throws Exception {
		WorkFlowGraph wg = gdao.queryGraphById(graphId);
		
		List<TaskArea> ts = tdao.queryTaskAreas(graphId);
		List<Line> ls = ldao.queryLines(graphId);
		
		wg.setTasks(ts);
		wg.setLines(ls);
		
		return wg;
	}
	
	/**
	 * 保存流程图信息
	 * @param graph
	 * @throws Exception
	 */
	public void savaGraph(WorkFlowGraph graph) throws Exception {
		//1、保存graph信息
		int graphId = gdao.savaGraph(graph);
		
		//2、保存taskArea信息
		List<TaskArea> tasks = graph.getTasks();
		if(tasks != null && tasks.size() > 0) {
			tdao.savaTaskArea(tasks, graphId);
		}
		//3、保存line信息
		List<Line> lines = graph.getLines();
		if(tasks != null && lines.size() > 0) {
			ldao.savaLine(lines, graphId);
		}
	}
	
	/**
	 * 更新流程图信息
	 * @param graph
	 * @throws Exception
	 */
	public void updateGraph(WorkFlowGraph graph) throws Exception {
		//更新graph
		gdao.updateGraph(graph);
		
		List<TaskArea> tasks = graph.getTasks();
		List<Line> lines = graph.getLines();
		
		//更新taskArea信息
		if(tasks != null) {
			List<Integer> ids = tdao.queryTaskAreaIds(graph.getGraphId());
			
			List<TaskArea> save = new ArrayList<TaskArea>();
			List<TaskArea> update = new ArrayList<TaskArea>();
			for (int i = 0; i < tasks.size(); i++) {
				if(ids.contains(tasks.get(i).getId())) {
					update.add(tasks.get(i));
					ids.remove(new Integer(tasks.get(i).getId()));
				}else {
					save.add(tasks.get(i));
				}
			}
			if(save.size() > 0) {
				tdao.savaTaskArea(save, graph.getGraphId());
			}
			if(update.size() > 0) {
				tdao.updateTaskArea(update);
			}
			//删除其他task
			for (int i = 0; i < ids.size(); i++) {
				tdao.deleteTaskAreaById(ids);
			}
		}
		
		//更新Line数据
		if(lines != null) {
			List<Integer> ids = ldao.queryLineIds(graph.getGraphId());
			
			List<Line> save = new ArrayList<Line>();
			List<Line> update = new ArrayList<Line>();
			for (int i = 0; i < lines.size(); i++) {
				if(ids.contains(lines.get(i).getId())) {
					update.add(lines.get(i));
					ids.remove(new Integer(lines.get(i).getId()));
				}else {
					save.add(lines.get(i));
				}
			}

			if(save.size() > 0) {
				ldao.savaLine(save, graph.getGraphId());
			}
			if(update.size() > 0) {
				ldao.updateLine(update);
			}
			//删除其他线条
			for (int i = 0; i < ids.size(); i++) {
				ldao.deleteLineById(ids);
			}
		}
	}
	
	/**
	 * 删除流程图
	 * @param graphId
	 * @throws Exception
	 */
	public void deleteGraph(int graphId) throws Exception {
		gdao.deleteGraph(graphId);
		tdao.deleteTaskAreaByGraphId(graphId);
		ldao.deleteLineByGraphId(graphId);
	}
	
	/**
	 * 根据用户角色查询其可以发起的流程列表
	 * @param role
	 * @return
	 * @throws Exception
	 */
	public List<WorkFlowGraph> queryStartWFByRole(String role) throws Exception {
		
		return gdao.queryStartWFByRole(role);
	}
}
